﻿using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RStein.Posterous.API;
using RStein.PosterousReader.WP.HostServices;
using RStein.PosterousReader.WP.ViewModels.ViewObjects;
using  RStein.PosterousReader.Common;
using RStein.PosterousReader.WP.Extensions;
using System.Threading;

namespace RStein.PosterousReader.WP.ViewModels
{
  public class PostAddContainerViewModel : PosterousViewModelBase
  {
    public static readonly string QUERY_PARAMETER_SITE = "site";
    
    public readonly string DEFAULT_SUBJECT = "Můj COOL příspěvek";
    public readonly string DEFAULT_BODY = "Tak tady je text....";
    
    private PostVO m_post;
    private bool m_isSaveInProgress;
    
    public PostAddContainerViewModel(IPosterousApplication posterousApplication, INavigationService navigation)
      : this(posterousApplication, navigation, GlobalConstants.PAGE_ADD_POST_TITLE)
    {
      
    }

    public PostAddContainerViewModel(IPosterousApplication posterousApplication, INavigationService navigation, string pageTitle)
      : base(posterousApplication, navigation, pageTitle)
    {
      SaveCommand = new DelegateCommand<object>(_ => ThreadPool.QueueUserWorkItem(__ => saveData()), _ => IsAllInitDataLoaded() && ValidateData());
      TextChangedAction = () => RaisePropertyChangedEvent(() => SaveCommand);
    }

    public Action TextChangedAction
    {
      get;
      private set;
    }

    public PostVO CurrentPost
    {
      get
      {
        return m_post;
      }
      set
      {
        m_post = value;
        RaisePropertyChangedEvent(() => CurrentPost);
        
      }
    }

    public bool IsSavePostInProgress
    {
      get
      {
        return m_isSaveInProgress;
      }
      private set
      {
        m_isSaveInProgress = value;
        RaisePropertyChangedEvent(() => IsSavePostInProgress);
      }
    }

    public ICommand SaveCommand
    {
      get;
      private set;
    }

    public int TargetSiteId
    {
      get;
      set;
    }
    
    public override bool ValidateData()
    {
      return CurrentPost != null &&
             !String.IsNullOrEmpty(CurrentPost.Title) &&
             !String.IsNullOrEmpty(CurrentPost.Body) &&
             CurrentPost.Title != DEFAULT_SUBJECT &&
             CurrentPost.Body != DEFAULT_BODY;
    }
    
    protected override void DoInternalInit()
    {
      base.DoInternalInit();
      
      IsSavePostInProgress = false;
      extractPostSite();      
      CurrentPost = new PostVO()
                    {
                      SynchContext = SynchronizationContext.Current,
                      Body = DEFAULT_BODY,
                      Title = DEFAULT_SUBJECT
                    };
      
    }
    
        
    private void saveData()
    {
      IPosterousAccount account  = PosterousApplication.Current.GetOrAddCachedPosterousAccount(LastUsedUserName, LastUsedPassword);
            
      IPosterousSite targetSite = account.Sites.Where(site => site.Id == TargetSiteId).First();
      IPosterousPost post = GetTargetPost(targetSite);
      
      IsSavePostInProgress = true;      
      
      post.SaveChanges();

      IsSavePostInProgress = false;
      
      NavigationService.GoBack();
    
    }

    protected virtual  IPosterousPost GetTargetPost(IPosterousSite targetSite)
    {
      IPosterousPost post = targetSite.CreatePost(CurrentPost.Title, CurrentPost.Body, false);
      return post;
    }

    
    private void extractPostSite()
    {
      string siteIdString = NavigationService.GetQueryParameter(QUERY_PARAMETER_SITE);
      if (String.IsNullOrEmpty(siteIdString))
      {
        throw new ArgumentException("Invalid site id");
      }

      TargetSiteId = Int32.Parse(siteIdString);
    }
    
  }
}
